<!DOCTYPE html>
<html>
<head>
<title>菜单管理</title>
<link rel="stylesheet" th:href="@{/}+'layuiadmin/style/admin.css'" media="all">
<script src="js/day.js"></script>
<link rel="stylesheet" href="js/zTree/css/zTreeStyle/zTreeStyle.css" media="all">
<script type="text/javascript" src="js/zTree/js/jquery.ztree.all.min.js"></script>
<link rel="stylesheet" th:href="@{/}+'js/jquery.mloading/jquery.mloading.css'" media="all">
<script th:src="@{/}+'js/jquery.mloading/jquery.mloading.js'"></script>
<script th:src="@{/}+'js/tool.js'"></script>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">  
    <div class="layui-card-body">
      <div class="layui-row">
    <div class="layui-row layui-col-space10">
    <div class="layui-col-md5">
      <table id="org-table" lay-filter="org-table"></table>
    </div>
    <div class="layui-col-md7">
    <div class="layui-btn-group">
    <button type="button" class="layui-btn" data-type="assign">保存</button>
  </div>
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    </div>
  </div>
    </div>
  </div>
</div>

<script th:inline="none">
layui.use(['table','form'], function(){
  var form = layui.form
  ,table = layui.table;
  table.render({
	    elem: '#org-table'
	    ,url: 'org/getList?orgType=head' 
	    ,cols: [[
	      {type: 'checkbox'}
	      ,{field: 'id', title: 'ID',hide:true}
	      ,{field: 'name', width: 300, title: '公司名称'}
	    ]]
	    ,page: true
	    ,limit: 30
	    ,height: '600px'
	    ,text: '对不起，加载出现异常！'
  });
  table.on("row(org-table)",function(obj){
	  clickLayTableRow(table,form,'org-table',obj);
	  reloadTree();
  })
  var treeSetting = {
		async: {
			enable: true,
			url:"navi/getFullNaviTree",
			autoParam: ["id=pid"]
		},
		view:{
			showTitle: true,
			selectedMulti : false
		},check:{
			enable:true
		},callback:{
			onAsyncSuccess: function(){
				var orgId = getOrgId();
				if(orgId){
					$.ajax({
			    		url:"navi/getOrgNaviList",
			    		type:'POST',
			    		dataType:'json',
			    		data:{orgId: orgId},
			    		success:function(ret){
			    			let data = ret.data;
			    			for(var i=0;i<data.length;i++){
			    				var nodes =  treeObj.getNodesByParam("id",data[i].id,null);
			    				if(nodes.length>0){
			    					treeObj.checkNode(nodes[0],true,false);
						  			treeObj.expandNode(nodes[0], true, false, true);
			    				}
					  			
			    			}
			    		  }
			    	  })
				}
			}
		}
  };
  $.fn.zTree.init($("#treeDemo"), treeSetting);
  window.treeObj = $.fn.zTree.getZTreeObj("treeDemo");
  
  window.getOrgId = function(){
	  var checkStatus = table.checkStatus('org-table')
      ,checkData = checkStatus.data; 
	 
	  if(checkData.length === 0){
	     return "";
	  }
	  return checkData[0].id;
  }
  
  var action = {
	
	assign: function(){
    	var orgId = getOrgId();
    	if(orgId==""){
    		return layer.msg("请选择公司",{icon:0});
    	}
    	var btn = $(this).attr("disabled",true).addClass("layui-btn-disabled");
    	var naviIds = [];
    	var nodes = treeObj.getCheckedNodes(true);
    	for(let i=0;i<nodes.length;i++){
    		naviIds[i] = nodes[i].id;
    	}
    	$.ajax({
			url:'navi/assignOrgNavis',
			data: {naviIds: naviIds,orgId: orgId},
			type:'POST',
			dataType:'json',
			success: function(ret){
				treeObj.reAsyncChildNodes(null, "refresh");
				btn.attr("disabled",false).removeClass("layui-btn-disabled");
			}
		})    
    }
  };
  $('.layui-btn').each(function(i,obj){
	  $(this).on('click', function(){
		   var type = $(this).data('type');
		   action[type].call(this);
	  })
  });
});
var reloadTree = function(){
	 treeObj.reAsyncChildNodes(null, "refresh");
}
</script>
</body>